﻿using System;
using System.IO;
using System.Text;
using System.Xml;

namespace WPay.Infrastructure.Unity.H5Pay
{
  /// <summary>
  /// 错误的支付方式代码
  /// </summary>
  public sealed class SubmitLog {
    /// <summary>
    /// 软件未注册
    /// </summary>
    /// <returns></returns>
    public XmlDocument RegPay() {
      XmlDocument ls = new XmlDocument();
      ls.LoadXml( "<WU_ROOT><HEAD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
                                + "<TYPE>SYSTEM_ERROR</TYPE><CODE>200</CODE><TITLE>软件未注册</TITLE><MESSAGE>请联系软件开发商注册软件.</MESSAGE>"
                                + "</HEAD><BODY /></WU_ROOT>" );
      return ls;

    }// end

    /// <summary>
    /// 错误的支付方式代码
    /// </summary>
    /// <param name="auth_type"></param>
    /// <returns></returns>
    public XmlDocument NoPay( String auth_type ) {
      XmlDocument ls = new XmlDocument();
      ls.LoadXml( "<WU_ROOT><HEAD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
                                + "<TYPE>REMOTEACCESS</TYPE><CODE>200</CODE><TITLE>支付方式错误</TITLE><MESSAGE>不支持本支付方式代码:" + auth_type + "</MESSAGE>"
                                + "</HEAD><BODY /></WU_ROOT>" );
      return ls;

    }// end

    /// <summary>
    /// 日志记录
    /// </summary>
    /// <param name="name">支付名称</param>
    /// <param name="msg">日志信息</param>
    public static void Log( String name, String msg ) {
      // 日志文件
      String logFile = AppDomain.CurrentDomain.BaseDirectory + @"\logs\" + name + DateTime.Now.ToString("yyyy-MM-dd") + ".log";

      // 检查文件夹
      if( !Directory.Exists( Path.GetDirectoryName( logFile ) ) ) {
        try {
          Directory.CreateDirectory( Path.GetDirectoryName( logFile ) );

        } catch( Exception ) {
          return;

        }

      }

      // 写入文件
      try {
        File.AppendAllText( logFile, DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss fff" ) + "\t" + msg + "\r\n", Encoding.UTF8 );

      } catch( Exception ) {
      }

    }// end

  }

}